export interface FileTypeConfig {
  mimeType: string;
  extensions: string[];
  maxSize?: number; // 特定类型的最大文件大小（字节）
  category: 'image' | 'document' | 'presentation' | 'spreadsheet' | 'archive' | 'video' | 'audio';
}

// 文件类型配置
export const allowedFileTypes: FileTypeConfig[] = [
  // 图片类型
  {
    mimeType: 'image/jpeg',
    extensions: ['.jpg', '.jpeg'],
    maxSize: 5 * 1024 * 1024, // 5MB
    category: 'image'
  },
  {
    mimeType: 'image/png',
    extensions: ['.png'],
    maxSize: 5 * 1024 * 1024, // 5MB
    category: 'image'
  },
  {
    mimeType: 'image/gif',
    extensions: ['.gif'],
    maxSize: 5 * 1024 * 1024, // 5MB
    category: 'image'
  },
  {
    mimeType: 'image/webp',
    extensions: ['.webp'],
    maxSize: 5 * 1024 * 1024, // 5MB
    category: 'image'
  },
  
  // 文档类型
  {
    mimeType: 'application/pdf',
    extensions: ['.pdf'],
    maxSize: 20 * 1024 * 1024, // 20MB
    category: 'document'
  },
  {
    mimeType: 'application/msword',
    extensions: ['.doc'],
    maxSize: 20 * 1024 * 1024, // 20MB
    category: 'document'
  },
  {
    mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    extensions: ['.docx'],
    maxSize: 20 * 1024 * 1024, // 20MB
    category: 'document'
  },
  {
    mimeType: 'text/plain',
    extensions: ['.txt'],
    maxSize: 2 * 1024 * 1024, // 2MB
    category: 'document'
  },
  {
    mimeType: 'application/rtf',
    extensions: ['.rtf'],
    maxSize: 10 * 1024 * 1024, // 10MB
    category: 'document'
  },
  
  // 演示文稿类型
  {
    mimeType: 'application/vnd.ms-powerpoint',
    extensions: ['.ppt'],
    maxSize: 50 * 1024 * 1024, // 50MB
    category: 'presentation'
  },
  {
    mimeType: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    extensions: ['.pptx'],
    maxSize: 50 * 1024 * 1024, // 50MB
    category: 'presentation'
  },
  
  // 电子表格类型
  {
    mimeType: 'application/vnd.ms-excel',
    extensions: ['.xls'],
    maxSize: 10 * 1024 * 1024, // 10MB
    category: 'spreadsheet'
  },
  {
    mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    extensions: ['.xlsx'],
    maxSize: 10 * 1024 * 1024, // 10MB
    category: 'spreadsheet'
  },
  {
    mimeType: 'text/csv',
    extensions: ['.csv'],
    maxSize: 5 * 1024 * 1024, // 5MB
    category: 'spreadsheet'
  },
  
  // 压缩文件类型
  {
    mimeType: 'application/zip',
    extensions: ['.zip'],
    maxSize: 100 * 1024 * 1024, // 100MB
    category: 'archive'
  },
  {
    mimeType: 'application/x-rar-compressed',
    extensions: ['.rar'],
    maxSize: 100 * 1024 * 1024, // 100MB
    category: 'archive'
  },
  {
    mimeType: 'application/x-7z-compressed',
    extensions: ['.7z'],
    maxSize: 100 * 1024 * 1024, // 100MB
    category: 'archive'
  }
];

// 获取所有允许的MIME类型
export const getAllowedMimeTypes = (): string[] => {
  return allowedFileTypes.map(type => type.mimeType);
};

// 获取所有允许的文件扩展名
export const getAllowedExtensions = (): string[] => {
  const extensions = new Set<string>();
  allowedFileTypes.forEach(type => {
    type.extensions.forEach(ext => extensions.add(ext));
  });
  return Array.from(extensions);
};

// 根据MIME类型获取文件配置
export const getFileTypeConfig = (mimeType: string): FileTypeConfig | undefined => {
  return allowedFileTypes.find(type => type.mimeType === mimeType);
};

// 根据文件扩展名获取文件配置
export const getFileTypeConfigByExtension = (extension: string): FileTypeConfig | undefined => {
  const ext = extension.toLowerCase();
  return allowedFileTypes.find(type => type.extensions.includes(ext));
};

// 验证文件类型
export const validateFileType = (mimeType: string, extension: string): { isValid: boolean; message?: string } => {
  const config = getFileTypeConfig(mimeType);
  
  if (!config) {
    return { isValid: false, message: `不支持的文件类型: ${mimeType}` };
  }
  
  const ext = extension.toLowerCase();
  if (!config.extensions.includes(ext)) {
    return { isValid: false, message: `文件扩展名与MIME类型不匹配: ${ext} 不是 ${mimeType} 的有效扩展名` };
  }
  
  return { isValid: true };
};

// 获取特定类别的文件类型
export const getFileTypesByCategory = (category: string): FileTypeConfig[] => {
  return allowedFileTypes.filter(type => type.category === category);
};

// 默认文件大小限制（如果特定类型没有设置）
export const DEFAULT_FILE_SIZE_LIMIT = 10 * 1024 * 1024; // 10MB